using System;
using Common.Commands;
using Common.Commands.NoteCommands;
using Core.Domain.Model;
using Dto.ApiResponses.NoteResponses;
using Sanelib.DataOperation;

namespace Core.Processors.NoteProcessors
{
    public class AddNoteProcessor : ICommandProcessor<AddNote>
    {
        private readonly IRepository<Note> _noteRepository;
        private readonly IRepository<Member> _memberRepository;

        public AddNoteProcessor(IRepository<Note> noteRepository, IRepository<Member> memberRepository)
        {
            _noteRepository = noteRepository;
            _memberRepository = memberRepository;
        }

        public dynamic Process(AddNote command)
        {
            var user = _memberRepository.GetById(command.UserId);

            var note = new Note
            {
                Id = command.Id,
                Name = "Note",
                EntityType = command.EntityType,
                ReferenceId = command.ReferenceId,
                ReferenceName = command.ReferenceName,
                Description = command.Description,
                ImageData = command.ImageData,
                CreatedBy = command.UserId
            };
            _noteRepository.Add(note);

            var response = new NoteResponse
            {
                Id = command.Id,
                CreatedOn = DateTime.Now,
                CreatedByName = user.Name,
                Description = command.Description,
                Name = note.Name
            };

            return response;
        }
    }
}
